package view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import model.CameraManager;

import java.io.IOException;

import controller.App;

/**
 * Camera preview view. Shows camera preview data
 */
public class CameraPreviewView extends SurfaceView implements SurfaceHolder.Callback 
{
    private static final String TAG = CameraPreviewView.class.getSimpleName();

    /**
     * Surface holder for camera preview data
     */
    private SurfaceHolder surfaceHolder;
    private float[] coords;
    private Paint paint = new Paint();

	public CameraPreviewView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
    }
    
    public void setCoords(float[] coords)
    {
    	this.coords = coords;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
    	setWillNotDraw(false);
    	
        try 
        {
        	CameraManager cameraManager = CameraManager.getInstance();
            cameraManager.setCameraDisplayOrientation((Activity) getContext());
            cameraManager.getCamera().setPreviewDisplay(holder);
            cameraManager.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "SurfaceCreated() - Error setting camera preview: " + e.toString());
        }
    }

    // implementing this method is mandatory, but we don't have a use for it so.
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
    {
    	// empty method, see explanation above.
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
    	
    	CameraManager cameraManager = CameraManager.getInstance();

        if (surfaceHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

        restartPreviewSurface(cameraManager);
    }
    
    private void restartPreviewSurface(CameraManager cameraManager)
    {
    	// stop preview before making changes
        
        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try 
        {
        	cameraManager.stopPreview();
        	
            cameraManager.setCameraDisplayOrientation((Activity) getContext());
            cameraManager.getCamera().setPreviewDisplay(surfaceHolder);
            cameraManager.startPreview();

        } catch (Exception e) {
            Log.d(TAG, "SurfaceDestroyed"
            		+ "() - Error starting camera preview: " + e.toString());
        }
    }
	@Override
    protected void onDraw(Canvas canvas) 
    {
    	super.onDraw(canvas);
    	if(coords == null)
    		return;
    
    	Log.d("QR", String.format("Coords QR: {%d;%d}",(int) coords[0], (int) coords[1]));
    	
		paint.setColor(Color.WHITE);
		paint.setStrokeWidth(30);
		canvas.drawPoint(coords[0], coords[1], paint);
    }}
